// 设置localstorage
export const setLocalStorage = function (key, val) {
  uni.setStorageSync(key, val)
}
// 获取localstorage
export const getLocalStorage = function (key) {
  return uni.getStorageSync(key)
}
// 获取localstorage
export const clearLocalStorage = function (key) {
  uni.clearStorageSync(key)
}
// 路由跳转
export const redirectTo = function (url, options = {}) {
  uni.redirectTo({ url, ...options })
}
// 路由跳转
export const reLaunch = function (url, options = {}) {
  uni.reLaunch({ url, ...options })
}
// 路由跳转
export const navigateTo = function (url, options = {}) {
  uni.navigateTo({ url, ...options })
}

export const navigateBack = function (delta = 1) {
  return new Promise((resolve, reject) => {
    uni.navigateBack({
      delta: delta,
      success: (res) => resolve(res),
      fail: (err) => reject(err)
    })
  })
}
// 微信登录获取code
export const wxLogin = function () {
  return new Promise((resolve, reject) => {
    uni.login({
      success: (res) => resolve(res.code),
      fail: (err) => reject(err)
    })
  })
}

// 设置网页标题
export const setNavigationBarTitle = function (title) {
  uni.setNavigationBarTitle({ title })
}
// 显示提示信息
export const showToast = function (title, icon = 'none', duration = 2000) {
  uni.showToast({ title, icon, duration })
}
// 加载中
export const showLoading = function (title = '加载中') {
  return new Promise((resolve, reject) => {
    wx.showLoading({
      title,
      mask: true,
      success: (res) => resolve(res),
      fail: (err) => reject(err)
    })
  })
}
// 关闭loading
export const hideLoading = function () {
  wx.hideLoading()
}
// 获取当前页面
export const getCurPage = function (index = 0) {
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1 + index]
  return currentPage
}
// 获取服务类型
export const getProvider = function () {
  return new Promise((resolve, reject) => {
    uni.getProvider({
      service: 'payment',
      success: (res) => resolve(res.provider),
      fail: () => reject('获取服务类型失败')
    })
  })
}
// 支付
export const requestPayment = function (params = {}) {
  let { from } = params
  return new Promise((resolve, reject) => {
    uni.requestPayment({
      ...params,
      success: (res) =>
        resolve(
          uni.showToast({
            title: '已支付成功',
            duration: 2000,
            success: () => {
              setTimeout(() => {
                uni.navigateTo({
                  url: ''
                })
              }, 2000)
            }
          })
        ),
      fail: function (res) {
        if (res.errMsg == 'requestPayment:fail cancel') {
          return uni.showToast({
            icon: 'error',
            title: '取消支付',
            duration: 2000,
            success: () => {
              setTimeout(() => {
                if (from != 'orderPage') {
                  return uni.navigateTo({
                    url: ''
                  })
                }
              }, 2000)
            }
          })
        }
      }
    })
  })
}

// 获取设备信息
export const getSystemInfo = function () {
  return new Promise((resolve, reject) => {
    wx.getSystemInfo({
      success: function (res) {
        let clientWidth = res.windowWidth
        let clientHeight = res.windowHeight
        return resolve(res)
      },
      fail: (err) => reject(err)
    })
  })
}

// function uploadFile(avatarUrl, name) {
//   return new Promise((resolve, reject) => {
//     wx.uploadFile({
//       url: host + api +"/upload/avatar", //服务器地址
//       filePath: avatarUrl, //文件临时路径
//       name: "file",
//       header: {
//         Authorization: token(),
//         "Content-Type": "multipart/form-data",
//       },
//       formData: {
//         file: avatarUrl,
//       },
//       success: (res) => {
//         console.log(res, "asdasdas");
//         resolve(res.data)
//       },
//       fail: (error) => {
//         // uni.hideLoading();
//         // console.log(error);
//       },
//     });
//   });
// }